package cn.com.winning.ecare.activity;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.apache.http.NameValuePair;
import org.apache.http.message.BasicNameValuePair;
import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import android.app.Activity;
import android.app.AlertDialog;
import android.app.Dialog;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.os.Handler;
import android.os.Looper;
import android.os.Message;
import android.view.Display;
import android.view.KeyEvent;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.Window;
import android.view.View.OnClickListener;
import android.view.WindowManager;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.BaseAdapter;
import android.widget.Button;
import android.widget.EditText;
import android.widget.GridView;
import android.widget.ImageButton;
import android.widget.LinearLayout;
import android.widget.ListView;
import android.widget.RatingBar;
import android.widget.RelativeLayout;
import android.widget.SimpleAdapter;
import android.widget.TextView;
import android.widget.Toast;
import cn.com.winning.ecare.common.Grideviewmenu;
import cn.com.winning.ecare.common.Listviewheight;
import cn.com.winning.ecare.common.MyApplication;
import cn.com.winning.ecare.utils.DisplayUtil;
import cn.com.winning.ecare.utils.HTTPGetTool;
import cn.com.winning.ecare.utils.SlidingLayout;
import cn.com.winning.ecare.utils.URLUtils;
import cn.com.winning.ecare.utils.UserRecord;
import cn.com.winning.ecare.widgets.CustomProgressDialog;

/**意见建议
 * @author Administrator
 *
 */
public class Suggestion extends Activity {

	private EditText editSuggestion; // 意见内容
	private Button btn_submit; // 提交评论内容
	private RelativeLayout btn_ks, btn_ys, btn_lx;// 选择条件
	private TextView text_ks, text_ys, text_lx;// 选择条件
	private String strSpinner1 = "", strSpinner2 = "", strSpinner3 = ""; // 记录选择spinner结果
	private String strSpinnerid1 = "", strSpinnerid2 = "", strSpinnerid3 = ""; // 记录选择spinnerid结果
	private String[] arr_rating; // 记录RatingBar的选择记录
	private CustomProgressDialog proDialog;// 加载框
	private List<String> list_pfId, list_pfMc;
	private Activity oThis;
	private String ksId;// 作为定位医生的条件

	MyApplication myApp;
	private List<Map<String, Object>> list_pllb, list_ks, list_ys,list_pf;

	private String[] arrLxMc, arrLxId, arrKsMc, arrKsId, arrYs;

	// 记录选项
	private int preId_lb = -1, preId_ks = -1, preId_ys = -1;
	private int preId_lb1 = -1, preId_ks1 = -1, preId_ys1 = -1;
	private MyAdapter_lb myAdapter_lb;
	private MyAdapter_ks myAdapter_ks;
	private MyAdapter_ys myAdapter_ys;

	// 回写信息
	private String pllbid = "", pllbmc = "", ksmc = "", ysmc = "", plnr = "",
			drzh = "";

	private Suggestion othis = this;
//	Grideviewmenu gm;
//	private GridView gridView;
	
	/**
	 * 侧滑布局对象，用于通过手指滑动将左侧的菜单布局进行显示或隐藏。
	 */
//	private SlidingLayout slidingLayout;
	
	private LinearLayout content;

	/**
	 * menu按钮，点击按钮展示左侧布局，再点击一次隐藏左侧布局。
	 */
	private ImageButton menuButton;
	private TextView nametv,tv;
	private int windowwidth,windowheight,listheight;
	
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		// TODO Auto-generated method stub
		super.onCreate(savedInstanceState);
		requestWindowFeature(Window.FEATURE_NO_TITLE);
		setContentView(R.layout.suggestion);
		myApp = (MyApplication) getApplication();
		myApp.addActivity(this);
		myApp.getUserRecords().setUsername(myApp.getAccount());
		myApp.getUserRecords().setModule("Suggestion@意见建议");
		UserRecord.getInstance(myApp.getUserRecords()).init();
		WindowManager windowManager = getWindowManager();
		Display display = windowManager.getDefaultDisplay();
		windowwidth = (int) (display.getWidth());
		windowheight = (int) (display.getHeight());
		listheight = windowheight*3/4;
		 //设置滑动菜单
		initSliding();
		// 获取控件
		findView();

		// 获取数据
		getData();

		// 设置监听
		setListener();
		
		

	}
	private void initSliding(){
		menuButton = (ImageButton) findViewById(R.id.menuButton);
		menuButton.setOnClickListener(new OnClickListener() {
			@Override
			public void onClick(View v) {

				showBackTip();
			}
		});

		
	}

	private void getData() {

		proDialog = CustomProgressDialog.createDialog(oThis);
		proDialog.setMessage("数据加载中...");
		proDialog.show();
		proDialog.setCanceledOnTouchOutside(false);
		// 这里单开一个线程和后台数据交互
		new Thread(new LoadData()).start();

	}

	class LoadData implements Runnable {

		public void run() {
			// TODO Auto-generated method stub
			Looper.prepare();
			iniPfData();
			iniPlData();
			iniKSData();
			Looper.loop();
		}
	}

	class LoadDataYs implements Runnable {

		public void run() {
			// TODO Auto-generated method stub
			Looper.prepare();
			iniYsData();
			Looper.loop();
		}
	}

	private void iniYsData() {
		try {
			Message msg = myHandler.obtainMessage();
			Bundle bundle = new Bundle();
			List<NameValuePair> ls = new ArrayList<NameValuePair>();
			ls.add(new BasicNameValuePair("ksid", ksId));
			JSONObject json = HTTPGetTool.getTool().post(
					URLUtils.HOST + URLUtils.SUGGESTIONYS, ls);

			if (json != null) {
				if (!json.toString().equals(
						"{\"msg\":\"系统错误！\",\"success\":\"false\"}")) {
					if (!json.getString("orglist").equals("")) {
						JSONArray jsonArr = new JSONArray(
								json.getString("orglist"));
						JSONObject js = null;
						// arrYs = new String[jsonArr.length()];
						for (int i = 0; i < jsonArr.length(); i++) {
							js = jsonArr.getJSONObject(i);
							// arrYs[i] = js.getString("text").toString();
							Map<String, Object> map = new HashMap<String, Object>();
							map.put("mc", js.getString("text").toString());
							list_ys.add(map);
						}

						bundle.putInt("type", 3);
						bundle.putString("msg", "");
					} else {
						bundle.putInt("type", -1);
						bundle.putString("msg", "暂无数据");
					}
				} else {
					bundle.putInt("type", -1);
					bundle.putString("msg", "系统错误");
				}
			} else {
				bundle.putInt("type", -1);
				bundle.putString("msg", "网络连接失败!");
			}
			msg.setData(bundle);
			myHandler.sendMessage(msg);
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
			Bundle bundle = new Bundle();
			bundle.putInt("type", -1);
			bundle.putString("msg", e.toString());
			Message msg = myHandler.obtainMessage();
			msg.setData(bundle);
			myHandler.sendMessage(msg);
		}
	}

	private void iniPfData() {
		try {
			Message msg = myHandler.obtainMessage();
			Bundle bundle = new Bundle();
			JSONObject json = HTTPGetTool.getTool().post(
					URLUtils.HOST + URLUtils.SUGGESTIONPF, null);

			if (json != null) {
				if (!json.toString().equals(
						"{\"msg\":\"系统错误！\",\"success\":\"false\"}")) {
					if (!json.getString("types").equals("")) {
						JSONArray jsonArr = new JSONArray(
								json.getString("types"));
						JSONObject js = null;
						for (int i = 0; i < jsonArr.length(); i++) {
							js = jsonArr.getJSONObject(i);
							list_pfId.add(js.getString("id").toString());
							list_pfMc.add(js.getString("xmlb").toString());
							Map<String, Object> map = new HashMap<String, Object>();
							map.put("id", js.getString("id").toString());
							map.put("xmmc", js.getString("xmlb").toString());
							list_pf.add(map);
						}

						bundle.putInt("type", 0);
						bundle.putString("msg", "");
					} else {
						bundle.putInt("type", -1);
						bundle.putString("msg", "暂无数据");
					}
				} else {
					bundle.putInt("type", -1);
					bundle.putString("msg", "系统错误");
				}
			} else {
				bundle.putInt("type", -1);
				bundle.putString("msg", "网络连接失败!");
			}
			msg.setData(bundle);
			myHandler.sendMessage(msg);
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
			Bundle bundle = new Bundle();
			bundle.putInt("type", -1);
			bundle.putString("msg", e.toString());
			Message msg = myHandler.obtainMessage();
			msg.setData(bundle);
			myHandler.sendMessage(msg);
		}

	}

	private void iniKSData() {
		try {
			Message msg = myHandler.obtainMessage();
			Bundle bundle = new Bundle();
			JSONObject json = HTTPGetTool.getTool().post(
					URLUtils.HOST + URLUtils.SUGGESTIONKS, null);

			if (json != null) {
				if (!json.toString().equals(
						"{\"msg\":\"系统错误！\",\"success\":\"false\"}")) {
					if (!json.getString("orglist").equals("")) {
						JSONArray jsonArr = new JSONArray(
								json.getString("orglist"));
						JSONObject js = null;
						for (int i = 0; i < jsonArr.length(); i++) {
							js = jsonArr.getJSONObject(i);
							Map<String, Object> map = new HashMap<String, Object>();
							map.put("mc", js.getString("orgname").toString());
							map.put("id", js.getString("id").toString());
							list_ks.add(map);
						}

						bundle.putInt("type", 2);
						bundle.putString("msg", "");
					} else {
						bundle.putInt("type", -1);
						bundle.putString("msg", "暂无数据");
					}
				} else {
					bundle.putInt("type", -1);
					bundle.putString("msg", "系统错误");
				}
			} else {
				bundle.putInt("type", -1);
				bundle.putString("msg", "网络连接失败!");
			}
			msg.setData(bundle);
			myHandler.sendMessage(msg);
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
			Bundle bundle = new Bundle();
			bundle.putInt("type", -1);
			bundle.putString("msg", e.toString());
			Message msg = myHandler.obtainMessage();
			msg.setData(bundle);
			myHandler.sendMessage(msg);
		}
	}

	private void iniPlData() {

		try {
			Message msg = myHandler.obtainMessage();
			Bundle bundle = new Bundle();
			JSONObject json = HTTPGetTool.getTool().post(
					URLUtils.HOST + URLUtils.SUGGESTIONPL, null);

			if (json != null) {
				if (!json.toString().equals(
						"{\"msg\":\"系统错误！\",\"success\":\"false\"}")) {
					if (!json.getString("types").equals("")) {
						JSONArray jsonArr = new JSONArray(
								json.getString("types"));
						JSONObject js = null;
						for (int i = 0; i < jsonArr.length(); i++) {
							js = jsonArr.getJSONObject(i);
							Map<String, Object> map = new HashMap<String, Object>();
							map.put("id", js.getString("id").toString());
							map.put("mc", js.getString("xmlb").toString());
							list_pllb.add(map);

						}

						bundle.putInt("type", 1);
						bundle.putString("msg", "");
					} else {
						bundle.putInt("type", -1);
						bundle.putString("msg", "暂无数据");
					}
				} else {
					bundle.putInt("type", -1);
					bundle.putString("msg", "系统错误");
				}
			} else {
				bundle.putInt("type", -1);
				bundle.putString("msg", "网络连接失败!");
			}
			msg.setData(bundle);
			myHandler.sendMessage(msg);
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
			Bundle bundle = new Bundle();
			bundle.putInt("type", -1);
			bundle.putString("msg", e.toString());
			Message msg = myHandler.obtainMessage();
			msg.setData(bundle);
			myHandler.sendMessage(msg);
		}
	}

	/**
	 * 主线程状态更新
	 */
	Handler myHandler = new Handler() {
		public void handleMessage(Message msg) {
			int type = msg.getData().getInt("type");
			switch (type) {
			case -1:
				proDialog.dismiss();
				Toast.makeText(oThis, msg.getData().getString("msg"),
						Toast.LENGTH_SHORT).show();
				break;
			case 0:
				proDialog.dismiss();
				setPfInfo();
				break;
			case 1:
				proDialog.dismiss();
				// iniSpinnerLx();
				// adapterLx.notdifyDataSetChanged();
				break;

			case 2:
				proDialog.dismiss();
				// iniSpinnerKs();
				// adapterLx.notdifyDataSetChanged();
				break;

			case 3:
				proDialog.dismiss();
				// iniSpinnerYs();

				break;

			default:
				break;
			}
		};
	};

	private void setPfInfo() {

		// 设置监听事件
		if (list_pfId.size() > 0) {//有评分项目才加载评分条
			arr_rating = new String[list_pfId.size()];
			
			ListView lv_pf = (ListView) this.findViewById(R.id.listview_pf);
			MySimpleAdapter myListAdapter = new MySimpleAdapter(this,list_pf,R.layout.listview_pflist,
					new String[]{"xmmc", "store_scores"},
					new int[]{R.id.pf_textview,R.id.ratingbar});
			lv_pf.setAdapter(myListAdapter);
			Listviewheight.setListViewHeightBasedOnChildren(lv_pf);
		}

	}

	private void setListener() {

		btn_lx.setOnClickListener(new View.OnClickListener() {

			@Override
			public void onClick(View v) {
				// TODO Auto-generated method stub

				final Dialog dialog = new Dialog(Suggestion.this, R.style.MyDialog);
				dialog.setContentView(R.layout.suggestion_lb);
				ListView listmx = (ListView) dialog.findViewById(R.id.suggestlist_lb);
				myAdapter_lb = new MyAdapter_lb(Suggestion.this);
				myAdapter_lb.setList(list_pllb);
				listmx.setAdapter(myAdapter_lb);
				TextView tv = (TextView) dialog.findViewById(R.id.title_lb);
				tv.setText("请选择类别");
				Window mWindow = dialog.getWindow();
				WindowManager.LayoutParams lp = mWindow.getAttributes();
				lp.width = windowwidth; // 设置宽度
			//	lp.height = windowheight;
				lp.height = listheight;//设置窗口高度
				mWindow.setAttributes(lp);
				
				//设置listview高度
				LinearLayout out = (LinearLayout)dialog.findViewById(R.id.suggestlist);
				LinearLayout.LayoutParams layoutp=(LinearLayout.LayoutParams)out.getLayoutParams(); 
				layoutp.height=listheight-DisplayUtil.dip2px(othis, 120);
				out.setLayoutParams(layoutp);
				//
				
				Button left = (Button) dialog.findViewById(R.id.dialog_button_ok);
				left.setOnClickListener(new View.OnClickListener() {

					@Override
					public void onClick(View v) {
						// TODO Auto-generated method stub
						
						text_lx.setText(strSpinner1);
						pllbid = strSpinnerid1;
						pllbmc = strSpinner1;
						preId_lb1 =preId_lb;

						dialog.dismiss();
					}
				});

				Button right = (Button) dialog.findViewById(R.id.dialog_button_cancel);
				right.setOnClickListener(new View.OnClickListener() {

					@Override
					public void onClick(View v) {
						// TODO Auto-generated method stub
						strSpinnerid1="";
						strSpinner1="";
						preId_lb=preId_lb1;
						dialog.dismiss();
						// MainActivity.this.finish();

					}
				});
				
				dialog.show();
				listmx.setOnItemClickListener(new OnItemClickListener() {

					@Override
					public void onItemClick(AdapterView<?> arg0, View arg1,
							int arg2, long arg3) {
						// TODO Auto-generated method stub
						arg1.setBackgroundColor(getResources().getColor(
								R.color.listheadtext_color));
						myAdapter_lb.notifyDataSetChanged();// 刷新
						TextView tv_id = (TextView) arg1
								.findViewById(R.id.tv_id);
						TextView tv_mc = (TextView) arg1
								.findViewById(R.id.tv_mc);
						tv_mc.setTextColor(getResources().getColor(R.color.white_bg));
						preId_lb = arg2;
						//
						strSpinner1 = tv_mc.getText().toString().trim();//临时记录listview选择的结果
						strSpinnerid1 = tv_id.getText().toString().trim();
								
					}
				});

			}
		});

		btn_ks.setOnClickListener(new View.OnClickListener() {

			@Override
			public void onClick(View v) {
				// TODO Auto-generated method stub

				final Dialog dialog = new Dialog(Suggestion.this, R.style.MyDialog);
				dialog.setContentView(R.layout.suggestion_lb);
				ListView listmx = (ListView) dialog.findViewById(R.id.suggestlist_lb);
				
				myAdapter_ks = new MyAdapter_ks(Suggestion.this);
				myAdapter_ks.setList(list_ks);
				listmx.setAdapter(myAdapter_ks);
				TextView tv = (TextView) dialog.findViewById(R.id.title_lb);
				tv.setText("请选择科室");
				Window mWindow = dialog.getWindow();
				WindowManager.LayoutParams lp = mWindow.getAttributes();
				lp.width = windowwidth; // 设置宽度
			//	lp.height = windowheight;
				lp.height = listheight;//设置窗口高度
				mWindow.setAttributes(lp);
				
				//设置listview高度
				LinearLayout out = (LinearLayout)dialog.findViewById(R.id.suggestlist);
				LinearLayout.LayoutParams layoutp=(LinearLayout.LayoutParams)out.getLayoutParams(); 
				layoutp.height=listheight-DisplayUtil.dip2px(othis, 120);
				out.setLayoutParams(layoutp);
				//
				Button left = (Button) dialog.findViewById(R.id.dialog_button_ok);
				left.setOnClickListener(new View.OnClickListener() {

					@Override
					public void onClick(View v) {
						// TODO Auto-generated method stub
						text_ks.setText(strSpinner2);
						ksId = strSpinnerid2;
						ksmc = strSpinner2;
						preId_ks1 =preId_ks;
						dialog.dismiss();
						// 获取科室下的医生
						if (!ksId.equals("")) {
							list_ys.clear();
							preId_ys = -1;
							getYsByKs();
						}
					}
				});

				Button right = (Button) dialog.findViewById(R.id.dialog_button_cancel);
				right.setOnClickListener(new View.OnClickListener() {

					@Override
					public void onClick(View v) {
						// TODO Auto-generated method stub
						strSpinnerid2="";
						strSpinner2="";
						preId_ks=preId_ks1;
						dialog.dismiss();
						// MainActivity.this.finish();

					}
				});
				dialog.show();

				listmx.setOnItemClickListener(new OnItemClickListener() {

					@Override
					public void onItemClick(AdapterView<?> arg0, View arg1,
							int arg2, long arg3) {
						// TODO Auto-generated method stub
						arg1.setBackgroundColor(getResources().getColor(
								R.color.listheadtext_color));
						myAdapter_ks.notifyDataSetChanged();// 刷新
						TextView tv_id = (TextView) arg1
								.findViewById(R.id.tv_id);
						TextView tv_mc = (TextView) arg1
								.findViewById(R.id.tv_mc);
						// strId = tv_id.getText().toString();
						tv_mc.setTextColor(getResources().getColor(R.color.white_bg));
						preId_ks = arg2;

						strSpinner2 = tv_mc.getText().toString().trim();
						strSpinnerid2 = tv_id.getText().toString().trim();
						

					}
				});

			}
		});

		btn_ys.setOnClickListener(new View.OnClickListener() {

			@Override
			public void onClick(View v) {
				// TODO Auto-generated method stub
				if (strSpinner2.equals("")) {
					Toast.makeText(Suggestion.this, "请选选择科室",
							Toast.LENGTH_SHORT).show();
					return;
				}

				final Dialog dialog = new Dialog(Suggestion.this, R.style.MyDialog);
				dialog.setContentView(R.layout.suggestion_lb);
				ListView listmx = (ListView) dialog
						.findViewById(R.id.suggestlist_lb);
				
				myAdapter_ys = new MyAdapter_ys(Suggestion.this);
				myAdapter_ys.setList(list_ys);
				listmx.setAdapter(myAdapter_ys);
				
				TextView tv = (TextView) dialog.findViewById(R.id.title_lb);
				tv.setText("请选择医生");
				Window mWindow = dialog.getWindow();
				WindowManager.LayoutParams lp = mWindow.getAttributes();
				lp.width = windowwidth; // 设置宽度
			//	lp.height = windowheight;
				lp.height = listheight;//设置窗口高度
				mWindow.setAttributes(lp);
				
				//设置listview高度
				LinearLayout out = (LinearLayout)dialog.findViewById(R.id.suggestlist);
				LinearLayout.LayoutParams layoutp=(LinearLayout.LayoutParams)out.getLayoutParams(); 
				layoutp.height=listheight-DisplayUtil.dip2px(othis, 120);
				out.setLayoutParams(layoutp);
				//
				Button left = (Button) dialog.findViewById(R.id.dialog_button_ok);
				left.setOnClickListener(new View.OnClickListener() {

					@Override
					public void onClick(View v) {
						// TODO Auto-generated method stub
						text_ys.setText(strSpinner3);
						ysmc = strSpinner3;
						preId_ys1 = preId_ys;
						dialog.dismiss();
					}
				});

				Button right = (Button) dialog.findViewById(R.id.dialog_button_cancel);
				right.setOnClickListener(new View.OnClickListener() {

					@Override
					public void onClick(View v) {
						// TODO Auto-generated method stub
						strSpinner3 = "";
						preId_ys = preId_ys1;
						dialog.dismiss();
						// MainActivity.this.finish();

					}
				});
				dialog.show();

				listmx.setOnItemClickListener(new OnItemClickListener() {

					@Override
					public void onItemClick(AdapterView<?> arg0, View arg1,
							int arg2, long arg3) {
						// TODO Auto-generated method stub
						arg1.setBackgroundColor(getResources().getColor(
								R.color.listheadtext_color));
						myAdapter_ks.notifyDataSetChanged();// 刷新
						// TextView tv_id = (TextView) arg1
						// .findViewById(R.id.tv_id);
						TextView tv_mc = (TextView) arg1
								.findViewById(R.id.tv_mc);
						tv_mc.setTextColor(getResources().getColor(R.color.white_bg));
						// strId = tv_id.getText().toString();
						
						preId_ys = arg2;

						strSpinner3 = tv_mc.getText().toString().trim();

						
					}
				});

			}
		});

		
		btn_submit.setOnClickListener(new View.OnClickListener() {

			@Override
			public void onClick(View v) {
				// TODO Auto-generated method stub
				String s1 = editSuggestion.getText().toString().trim();
				if (s1.equals("")) {
					Toast.makeText(Suggestion.this, "请填写完整再提交！",
							Toast.LENGTH_SHORT).show();
					return;
				}

				// 评分结果
				String pflbmc = "";
				String pf = "";//默认
				for (int i = 0; i < list_pfId.size(); i++) {
					pf = arr_rating[i]==null?"5.0":arr_rating[i];
					if (i == list_pfId.size() - 1) {
						pflbmc += list_pfId.get(i) + "," + list_pfMc.get(i)
								+ "," + pf;
					} else {
						pflbmc += list_pfId.get(i) + "," + list_pfMc.get(i)
								+ "," + pf + "/";
					}
				}

				// if(pflbmc.equals("")){
				// Toast.makeText(Suggestion.this, "评分", duration)
				// }

				List<NameValuePair> params = new ArrayList<NameValuePair>();
				params.add(new BasicNameValuePair("pflbmc", pflbmc));
				params.add(new BasicNameValuePair(" pllbid", pllbid));
				params.add(new BasicNameValuePair("pllbmc", pllbmc));
				params.add(new BasicNameValuePair("ksmc", ksmc));
				params.add(new BasicNameValuePair("ysmc", ysmc));
				params.add(new BasicNameValuePair("plnr", editSuggestion
						.getText().toString().trim()));
				params.add(new BasicNameValuePair("drzh", myApp.getAccount()));

				JSONObject res = HTTPGetTool.getTool().post(
						URLUtils.HOST + URLUtils.SUGGESTIONSAVE, params);
				String saveInfo = "SAVE_NOTSUCCESS";
				if (res != null) {
					try {
						saveInfo = res.get("yjInfo").toString();
					} catch (JSONException e) {
						// TODO Auto-generated catch block
						e.printStackTrace();
						Toast.makeText(Suggestion.this,
								"提交失败(原因：" + e.toString() + ")",
								Toast.LENGTH_SHORT).show();
					}
				}

				if (saveInfo.equals("SAVE_SUCCESS")) {
					Toast.makeText(Suggestion.this, "提交成功", Toast.LENGTH_SHORT)
							.show();
					// 到下一页面
					finish();
				} else if (saveInfo.equals("SAVE_NOTSUCCESS")) {
					Toast.makeText(Suggestion.this, "提交失败", Toast.LENGTH_SHORT)
							.show();
				}

			}
		});

	}

	class MyAdapter_lb extends BaseAdapter {

		private List<Map<String, Object>> list;
		private LayoutInflater mLayoutInflater;

		public MyAdapter_lb(Context context) {
			this.mLayoutInflater = LayoutInflater.from(context);
		}

		public List<Map<String, Object>> getList() {
			return list;
		}

		public void setList(List<Map<String, Object>> list) {
			this.list = list;
		}

		@Override
		public int getCount() {
			// TODO Auto-generated method stub
			return list.size();
		}

		@Override
		public Object getItem(int position) {
			// TODO Auto-generated method stub
			return null;
		}

		@Override
		public long getItemId(int position) {
			// TODO Auto-generated method stub
			return 0;
		}

		@Override
		public View getView(int position, View convertView, ViewGroup parent) {
			// TODO Auto-generated method stub

			ViewHolder holder = null;
			if (convertView == null) {
				convertView = mLayoutInflater.inflate(
						R.layout.suggestion_lb_item, null);
				holder = new ViewHolder();
				holder.tv_id = (TextView) convertView.findViewById(R.id.tv_id);
				holder.tv_mc = (TextView) convertView.findViewById(R.id.tv_mc);
				convertView.setTag(holder);

			} else
				holder = (ViewHolder) convertView.getTag();
			holder.tv_id.setText(list.get(position).get("id").toString());
			holder.tv_mc.setText(list.get(position).get("mc").toString());
			if (preId_lb == position){
				convertView.setBackgroundColor(getResources().getColor(
						R.color.listheadtext_color));
				holder.tv_mc.setTextColor(getResources().getColor(R.color.white_bg));
			}else{
				convertView.setBackgroundColor(getResources().getColor(
						R.color.white_bg));
				holder.tv_mc.setTextColor(getResources().getColor(R.color.textColor));
			}
			return convertView;
		}
	}

	class MyAdapter_ks extends BaseAdapter {

		private List<Map<String, Object>> list;
		private LayoutInflater mLayoutInflater;

		public MyAdapter_ks(Context context) {
			this.mLayoutInflater = LayoutInflater.from(context);
		}

		public List<Map<String, Object>> getList() {
			return list;
		}

		public void setList(List<Map<String, Object>> list) {
			this.list = list;
		}

		@Override
		public int getCount() {
			// TODO Auto-generated method stub
			return list.size();
		}

		@Override
		public Object getItem(int position) {
			// TODO Auto-generated method stub
			return null;
		}

		@Override
		public long getItemId(int position) {
			// TODO Auto-generated method stub
			return 0;
		}

		@Override
		public View getView(int position, View convertView, ViewGroup parent) {
			// TODO Auto-generated method stub

			ViewHolder holder = null;
			if (convertView == null) {
				convertView = mLayoutInflater.inflate(
						R.layout.suggestion_lb_item, null);
				holder = new ViewHolder();
				holder.tv_id = (TextView) convertView.findViewById(R.id.tv_id);
				holder.tv_mc = (TextView) convertView.findViewById(R.id.tv_mc);
				convertView.setTag(holder);

			} else
				holder = (ViewHolder) convertView.getTag();
			holder.tv_id.setText(list.get(position).get("id").toString());
			holder.tv_mc.setText(list.get(position).get("mc").toString());
			if (preId_ks == position){
				convertView.setBackgroundColor(getResources().getColor(
						R.color.listheadtext_color));
				holder.tv_mc.setTextColor(getResources().getColor(R.color.white_bg));
			}else{
				convertView.setBackgroundColor(getResources().getColor(
						R.color.white_bg));
				holder.tv_mc.setTextColor(getResources().getColor(R.color.textColor));
			}
			return convertView;
		}
	}

	class MyAdapter_ys extends BaseAdapter {

		private List<Map<String, Object>> list;
		private LayoutInflater mLayoutInflater;

		public MyAdapter_ys(Context context) {
			this.mLayoutInflater = LayoutInflater.from(context);
		}

		public List<Map<String, Object>> getList() {
			return list;
		}

		public void setList(List<Map<String, Object>> list) {
			this.list = list;
		}

		@Override
		public int getCount() {
			// TODO Auto-generated method stub
			return list.size();
		}

		@Override
		public Object getItem(int position) {
			// TODO Auto-generated method stub
			return null;
		}

		@Override
		public long getItemId(int position) {
			// TODO Auto-generated method stub
			return 0;
		}

		@Override
		public View getView(int position, View convertView, ViewGroup parent) {
			// TODO Auto-generated method stub

			ViewHolder holder = null;
			if (convertView == null) {
				convertView = mLayoutInflater.inflate(
						R.layout.suggestion_lb_item, null);
				holder = new ViewHolder();
				// holder.tv_id = (TextView)
				// convertView.findViewById(R.id.tv_id);
				holder.tv_mc = (TextView) convertView.findViewById(R.id.tv_mc);
				convertView.setTag(holder);

			} else
				holder = (ViewHolder) convertView.getTag();
			// holder.tv_id.setText(list.get(position).get("id").toString());
			holder.tv_mc.setText(list.get(position).get("mc").toString());
			if (preId_ys == position){
				convertView.setBackgroundColor(getResources().getColor(
						R.color.listheadtext_color));
				holder.tv_mc.setTextColor(getResources().getColor(R.color.white_bg));
			}else{
				convertView.setBackgroundColor(getResources().getColor(
						R.color.white_bg));
				holder.tv_mc.setTextColor(getResources().getColor(R.color.textColor));
			}
			return convertView;
		}
	}

	public class ViewHolder {
		private TextView tv_id;
		private TextView tv_mc;
	}

	private void getYsByKs() {
		proDialog = CustomProgressDialog.createDialog(oThis);
		proDialog.setMessage("数据加载中...");
		proDialog.show();
		proDialog.setCanceledOnTouchOutside(false);
		// 这里单开一个线程和后台数据交互
		new Thread(new LoadDataYs()).start();

	}

	private void findView() {
		editSuggestion = (EditText) this.findViewById(R.id.tv_suggestion);
		btn_submit = (Button) this.findViewById(R.id.btn_submit);

		btn_ks = (RelativeLayout) this.findViewById(R.id.btn_ks);
		btn_ys = (RelativeLayout) this.findViewById(R.id.btn_ys);
		btn_lx = (RelativeLayout) this.findViewById(R.id.btn_lx);
		text_ks = (TextView) this.findViewById(R.id.text_ks);
		text_ys = (TextView) this.findViewById(R.id.text_ys);
		text_lx = (TextView) this.findViewById(R.id.text_lx);
		
		list_pfId = new ArrayList<String>();
		list_pfMc = new ArrayList<String>();
		this.oThis = this;
		myApp = (MyApplication) getApplication();

		list_pllb = new ArrayList<Map<String, Object>>();
		list_ks = new ArrayList<Map<String, Object>>();
		list_ys = new ArrayList<Map<String, Object>>();
		list_pf = new ArrayList<Map<String, Object>>();
	}

	@Override
	public boolean onKeyDown(int keyCode, KeyEvent event) {
		if (keyCode == KeyEvent.KEYCODE_BACK && event.getRepeatCount() == 0) {
			showBackTip();
			return false;
		}
		return super.onKeyDown(keyCode, event);
	}
	
	/**
	 * 显示退出提示框
	 */
	private void showBackTip() {

		final Dialog dialog = new Dialog(this, R.style.MyDialog);

		dialog.setContentView(R.layout.dialog);
		TextView tv_title = (TextView) dialog.findViewById(R.id.title);
		tv_title.setText(R.string.exitTitle);

		TextView tv_message = (TextView) dialog.findViewById(R.id.message);
		tv_message.setText(R.string.exitTip);

		Button left = (Button) dialog.findViewById(R.id.left);
		left.setText(R.string.buttonOK);
		left.setOnClickListener(new View.OnClickListener() {

			@Override
			public void onClick(View v) {
				// TODO Auto-generated method stub
				dialog.dismiss();
				Suggestion.this.finish();
			}
		});

		Button right = (Button) dialog.findViewById(R.id.right);
		right.setText(R.string.buttonCancle);
		right.setOnClickListener(new View.OnClickListener() {

			@Override
			public void onClick(View v) {
				// TODO Auto-generated method stub
				dialog.dismiss();
				// MainActivity.this.finish();

			}
		});
		dialog.setCanceledOnTouchOutside(true);
		dialog.show();
		
	}
	
	/**
	 * 
	 * 自定义adapter 在listview中加入ratingbar
	 * @author Administrator
	 *
	 */
	class MySimpleAdapter extends SimpleAdapter {
		private int[] mTo;
	    private String[] mFrom;
	    private ViewBinder mViewBinder;
	    private List<? extends Map<String, ?>> mData;
	    private int mResource;
	    private int mDropDownResource;
	    private LayoutInflater mInflater;
		public MySimpleAdapter(Context context,
				List<? extends Map<String, ?>> data, int resource, String[] from,
				int[] to) {
			super(context, data, resource, from, to);
			mData = data;
	        mResource = mDropDownResource = resource;
	        mFrom = from;
	        mTo = to;
	        mInflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
		}
		 /**
	     * @see android.widget.Adapter#getView(int, View, ViewGroup)
	     */
	    public View getView(int position, View convertView, ViewGroup parent) {
	        return createViewFromResource(position, convertView, parent, mResource);
	    }
	    private View createViewFromResource(int position, View convertView,
	            ViewGroup parent, int resource) {
	        View v;
	        if (convertView == null) {
	            v = mInflater.inflate(resource, parent, false);

	            final int[] to = mTo;
	            final int count = to.length;
	            final View[] holder = new View[count];

	            for (int i = 0; i < count; i++) {
	                holder[i] = v.findViewById(to[i]);
	            }

	            v.setTag(holder);
	        } else {
	            v = convertView;
	        }
	        bindView(position, v);

	        return v;
	    }
	    
	    private void bindView(int position, View view) {
	        final Map dataSet = mData.get(position);
	        if (dataSet == null) {
	            return;
	        }
	        final ViewBinder binder = mViewBinder;
	        final View[] holder = (View[]) view.getTag();
	        final String[] from = mFrom;
	        final int[] to = mTo;
	        final int count = to.length;
	        final int num = position;//记录位置
	        for (int i = 0; i < count; i++) {
	            final View v = holder[i];       
	            if (v != null) {
	                final Object data = dataSet.get(from[i]);
	                String text = data == null ? "" : data.toString();
	                if (text == null) {
	                    text = "";
	                }
	                boolean bound = false;
	                if (binder != null) {
	                    bound = binder.setViewValue(v, data, text);
	                }
	                if (!bound) {
	                    if (v instanceof TextView) {
	                        ((TextView) v).setText(text);
	                    } 
	                    else if(v instanceof RatingBar){
	                    	((RatingBar)v).setOnRatingBarChangeListener(new RatingBar.OnRatingBarChangeListener() {

								@Override
								public void onRatingChanged(RatingBar ratingBar,
										float rating, boolean fromUser) {
									// TODO Auto-generated method stub
									arr_rating[num] = String.valueOf(rating);

								}
							});
	                    }
	                    else {
	                        throw new IllegalStateException(v.getClass().getName() + " is not a " +
	                                " view that can be bounds by this SimpleAdapter");
	                    }
	                }
	            }
	        }
	    }
	}
	
}
